package com.turistracker;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQuery.CachePolicy;

public class TuristrackerActivity extends FragmentActivity implements
		LocationListener {

	private GoogleMap mMap;
	private List<ParseObject> places;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_turistracker);

		mMap = ((SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map)).getMap();
		places = new ArrayList<ParseObject>();
		try {
			loadPlaces();
		} catch (ParseException e) {

			e.printStackTrace();
		}
		// Enabling MyLocation in Google Map
		mMap.setMyLocationEnabled(true);

		// Getting LocationManager object from System Service
		// LOCATION_SERVICE
		LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

		// Creating a criteria object to retrieve provider
		Criteria criteria = new Criteria();

		// Getting the name of the best provider
		String provider = locationManager.getBestProvider(criteria, true);

		// Getting Current Location From GPS
		Location location = locationManager.getLastKnownLocation(provider);

		if (location != null) {
			onLocationChanged(location);
		}

		locationManager.requestLocationUpdates(provider, 20000, 0, this);

		addMarkers();
		mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

			@Override
			public void onInfoWindowClick(Marker marker) {
				openPlaceInfo(marker);
			}
		});

	}

	private void openPlaceInfo(Marker marker) {
		Intent intent = new Intent(TuristrackerActivity.this,
				PlaceInfoActivity.class);
		intent.putExtra("PlaceName", marker.getTitle());
		startActivity(intent);

	}

	private void loadPlaces() throws ParseException {
		ParseQuery<ParseObject> query = ParseQuery.getQuery("Places");
		System.out.println(query.count());
		query.setCachePolicy(CachePolicy.IGNORE_CACHE);
		query.setLimit(1000);
		places.addAll(query.find());
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	private void addMarkers() {
		double lat = 0;
		double longi = 0;
		String title = null;
		String tipologia = "";
		System.out.println("size objects " + places.size());
		for (ParseObject parse : places) {
			lat = parse.getDouble("Latitud");
			longi = parse.getDouble("Longitud");
			title = parse.getString("Nombre");
			tipologia = parse.getString("Tipologia");
			mMap.addMarker(new MarkerOptions().position(new LatLng(lat, longi))
					.title(title).snippet(tipologia));
		}
	}

	@Override
	public void onLocationChanged(Location location) {
		LatLng latLng = new LatLng(location.getLatitude(),
				location.getLongitude());
		mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
		mMap.animateCamera(CameraUpdateFactory.zoomTo(12));
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
	}

	@Override
	public void onProviderEnabled(String provider) {
	}

	@Override
	public void onProviderDisabled(String provider) {

	}

}
